<template>
  <div class="dashboard-container">
    <div class="app-container">
      <el-card class="box-card">
        <el-tabs v-model="activeName">
          <el-tab-pane label="组织结构" name="first" class="tab-pane">
            <!--  el-row 头部 -->
            <el-row type="flex" justify="space-between" align="middle" class="department-header">
              <el-col :span="20">
                <i class="el-icon-s-home" />
                <span class="company">江苏传智播客教育科技股份有限公司</span></el-col>
              <el-col :span="4">
                <el-row type="flex" justify="end">
                  <el-col>负责人</el-col>
                  <el-col>
                    <el-dropdown>
                      <span class="el-dropdown-link">
                        操作<i class="el-icon-arrow-down el-icon--right" />
                      </span>
                      <el-dropdown-menu slot="dropdown">
                        <el-dropdown-item @click.native="addDept('')">添加子部门</el-dropdown-item>
                      </el-dropdown-menu>
                    </el-dropdown>
                  </el-col>
                </el-row>
              </el-col>
            </el-row>

            <!-- 主体区域绘制 -->
            <el-tree class="departments-tree" :data="treeData" :props="defaultProps" default-expand-all>
              <template slot-scope="{ data }">
                <el-row type="flex" justify="space-between" style="height: 50px; width: 100%;" align="middle">
                  <el-col :span="20">
                    <span>{{ data.name }}</span>
                  </el-col>
                  <el-col :span="4">
                    <el-row type="flex" justify="end">
                      <el-col>{{ data.manager }}</el-col>
                      <el-col>
                        <el-dropdown>
                          <span class="el-dropdown-link">
                            操作<i class="el-icon-arrow-down el-icon--right" />
                          </span>
                          <el-dropdown-menu slot="dropdown">
                            <el-dropdown-item @click.native="addDept(data.id)">添加子部门</el-dropdown-item>
                            <el-dropdown-item @click.native="editDept(data.id)">编辑部门</el-dropdown-item>
                            <el-dropdown-item v-if="data && !data.children" @click.native="delDept(data.id)">删除部门</el-dropdown-item>
                          </el-dropdown-menu>
                        </el-dropdown>
                      </el-col>
                    </el-row>
                  </el-col>
                </el-row>
              </template></el-tree>
          </el-tab-pane>
          <el-dialog
            :title="isEdit ? '编辑子部门' : '添加子部门'"
            :visible.sync="showDialog"
            width="50%"
            :close-on-click-modal="false"
            :close-on-press-escape="false"
          >
            <deptDialog
              v-if="showDialog"
              :parent-id="parentId"
              :is-edit="isEdit"
              :origin-list="originList"
              @update-depart="updateDepart"
              @close="showDialog=false"
            />
          </el-dialog>
        </el-tabs>

      </el-card>
    </div>
  </div>
</template>

<script>
import { getDepartments, delDepartment } from '@/api/departments'
import deptDialog from './deptDialog.vue'

import {
  tranListToTreeData
  // transData
} from '@/utils'

export default {
  name: 'Departments',
  components: {
    deptDialog
  },
  data() {
    return {
      activeName: 'first', // 被激活的tab页
      // 树形控件数据
      treeData: [],
      // 格式化属性结构显示
      defaultProps: {
        children: 'children',
        label: 'name'
      },
      showDialog: false, // 弹出层的显示隐藏
      parentId: '', // 父级id
      isEdit: false, // 是否是编辑
      originList: [] // 格式化需要传递给子组件的数据
    }
  },
  created() {
    // 初始化调用获取组织架构数据方法
    this.getDepartments()
  },
  methods: {
    // 删除部门
    async delDept(id) {
      // 操作弹窗验证 是否 confirm cancle
      const confirmres = await this.$confirm('此操作将永久删除该部门, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).catch(err => err)
      // 如果点击取消
      if (confirmres === 'cancle') return this.$message('您取消了删除')
      // 如果成功后 删除部门
      const res = await delDepartment(id)
      //  返回删除成功信息
      this.$message.success(res.message)

      // 重新获取数据
      this.getDepartments()
    },

    // 编辑部门
    editDept(id) {
      // 显示弹窗
      this.showDialog = true
      // 当前ID
      this.parentId = id
      // true是编辑
      this.isEdit = true
    },

    // 子类点击确定按钮后
    updateDepart() {
      // 弹窗关闭
      this.showDialog = false
      // 重新渲染数据
      this.getDepartments()
    },
    // 添加部门
    addDept(id) {
      // 显示弹窗
      this.showDialog = true
      // 当前id
      this.parentId = id
      // false 是添加
      this.isEdit = false
    },

    // 获取所有组织架构数据
    async getDepartments() {
      const res = await getDepartments()

      // 格式化需要传递给子组件的数据
      this.originList = res.data.depts.map(item => ({
        name: item.name,
        code: item.code,
        id: item.id,
        pid: item.pid
      }))

      // 递归算法的树形结构
      this.treeData = tranListToTreeData(res.data.depts, '')

      // 一般方法的树形结构 v-if="data && data.children.length===0"
      // this.treeData = transData(res.data.depts)
    }
  }
}
</script>

<style lang="scss" scoped>
.box-card {
  padding: 10px 30px;
}
.tab-pane {
  padding: 20px 80px;

  .company {
    margin-left: 12px;
    font-weight: bold;
    font-size: 14px;
  }

  .department-header {
    height: 50px;
    display: flex;
    align-items: center;
    border-bottom: 1px solid #ccc;
  }

  .departments-tree {
    margin: 12px 0px;
  }
}

</style>
